#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : Lee
# @File    : common_calc.py
# @Time    : 2024/1/11 16:26
import time
import math
import statistics


def format_time(time_list):
    res_list = list()
    for time_sec in time_list:
        time_sec = int(time_sec)
        if len(str(time_sec)) == 19:
            time_sec = int(time_sec / 1000000000)
        if len(str(time_sec)) == 16:
            time_sec = int(time_sec / 1000000)
        if len(str(time_sec)) == 13:
            time_sec = int(time_sec / 1000)
        res_list.append(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_sec)))
    return res_list


def calc_baseline_equation(point_a, point_b):
    # 计算A、B点直线方程式： ax + by + c = 0
    a = point_b[1] - point_a[1]
    b = point_a[0] - point_b[0]
    c = point_b[0] * point_a[1] - point_a[0] * point_b[1]
    return a, b, c


def get_offset_points(point_a, point_b, distance):
    """
    获取A、B两点之间的偏移点
    :param point_a: 点A
    :param point_b: 点B
    :param distance: 偏移距离
    :return: 偏移点
    """
    a, b, c = calc_baseline_equation(point_a, point_b)
    y_dis = math.sqrt(math.pow(-a * distance / b, 2) + math.pow(distance, 2))
    if distance > 0:
        return [[point_a[0], point_a[1] + y_dis], [point_b[0], point_b[1] + y_dis]]
    else:
        return [[point_a[0], point_a[1] - y_dis], [point_b[0], point_b[1] - y_dis]]



def get_variance(distances):
    """直线精度"""
    aver = statistics.mean(distances)
    result_sum = sum(map(lambda distance: math.pow(distance-aver, 2), distances))
    return math.sqrt(result_sum/(len(distances) - 1))


def get_connect_row_variance(distances, line_dis):
    """衔接行精度"""
    _h = sum([(_dis - line_dis) for _dis in distances]) / len(distances)
    __h = sum(map(lambda _dis: math.pow(_dis - line_dis - _h, 2), distances))
    return math.sqrt(__h / (len(distances) - 1)) * 100


def get_point_location_of_line(point, a, b, c):
    """
    判断点在直线的位置（左侧、右侧）
    直线方程为 ax + by + c = 0
    """
    if (a * point[0] + b * point[1] + c) >= 0:
        return "up"
    else:
        return "down"


def get_distance_of_two_point(point_a, point_b):
    """
    计算两点间距离
    """
    return ((point_a[0] - point_b[0]) ** 2 + (point_a[1] - point_b[1]) ** 2) ** 0.5


def get_distance_of_point_to_line(point, _a, _b, _c):
    """
    计算点到直线的距离
    """
    return abs(_a * point[0] + _b * point[1] + _c) / (_a ** 2 + _b ** 2) ** 0.5


def get_linear_equation_by_point_and_angle(point_a, angle):
    """
    已知一点和角度，求出直线方程
    """
    # y = k * x + b
    k = math.tan(angle)
    b = point_a[1] - k * point_a[0]
    return k, b


def get_intersection(point_a, point_b, point):
    """
    计算经过点point且垂直于直线point_a point_b，两条直线的交点
    :param point_a:
    :param point_b:
    :param point:
    :return:
    """
    if point_b[0] - point_a[0] == 0:
        # point_a point_b 构成垂线，那么垂直线就是一条水平线
        x = point_a[0]
        y = point[1]
    elif point_b[1] - point_a[1] == 0:
        # p1 p2 构成水平线，那么垂直线就是一条垂直线
        x = point[0]
        y = point_a[1]
    else:
        slope = (point_b[1] - point_a[1]) / (point_b[0] - point_a[0])
        perpendicular_slope = -1 / slope
        # intercept = point[1] - perpendicular_slope * point[0]
        x = (point[1] - point_a[1] + slope * point_a[0] - perpendicular_slope * point[0]) / (slope - perpendicular_slope)
        y = slope * (x - point_a[0]) + point_a[1]
    return x, y


def ior_calc_average_divide_line(start_point, end_point, count):
    """
    计算A、B点之间count等分后，每个点坐标
    """
    def f(x):
        return (start_point[1] * (end_point[0] - start_point[0]) + (x-start_point[0]) *
                (end_point[1] - start_point[1])) / (end_point[0] - start_point[0])

    _a_x = (end_point[0] - start_point[0]) / count

    _average_points = list()
    for i in range(count):
        _average_points.append([round(start_point[0] + i * _a_x, 6), round(f((start_point[0] + (i * _a_x))), 6)])
    _average_points.append(end_point)
    return _average_points


def get_virtual_point(point_a, point_start, point_end, angle):
    k, b = get_linear_equation_by_point_and_angle(point_a, angle)
    virtual_point_x = 0
    if point_a[0] > max(point_start[0], point_end[0]):
        virtual_point_x = min(point_start[0], point_end[0]) - 3
    elif point_a[0] < min(point_start[0], point_end[0]):
        virtual_point_x = max(point_start[0], point_end[0]) + 3
    else:
        if max(point_start[0], point_end[0]) - point_a[0] > point_a[0] - min(point_start[0], point_end[0]):
            virtual_point_x = max(point_start[0], point_end[0]) + 3
        else:
            virtual_point_x = min(point_start[0], point_end[0]) - 3
    return virtual_point_x, k * virtual_point_x + b


if __name__ == '__main__':
    theta = -2.9059113790516253
    offset = -0.008

    print(math.pi / 180 * offset + theta)